#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

#define PORT 9000

int main(int argc, char **argv)
{
	int x;
	struct sockaddr_in adr_inet;	// socket address
	const char *addr[] = {
		"44.135.86.12",
		"127.0.0.1",
		"172.16.23.95",
		"192.168.9.1"
	};

	unsigned long int net,hst;

	for(x=0; x<4; x++) {
		memset(&adr_inet, 0, sizeof(adr_inet));
		adr_inet.sin_family = AF_INET;
		adr_inet.sin_port = htons(PORT);
		if(! inet_aton(addr[x], &adr_inet.sin_addr))
			puts("Bad Address\n");

		// split address in host and network id
		hst = inet_lnaof(adr_inet.sin_addr);
		net = inet_netof(adr_inet.sin_addr);
	
		printf("%14s : net = 0x%08lX host = 0x%08lX\n", addr[x], net, hst);

		//zero the address and reconstruct from hst and net
		memset(&adr_inet, 0, sizeof(adr_inet));
		adr_inet.sin_family = AF_INET;
		adr_inet.sin_port = htons(PORT);
		adr_inet.sin_addr = inet_makeaddr(net, hst);

		printf("%14s : %s\n\n", "inet_makeaddr", inet_ntoa(adr_inet.sin_addr));
	}

	return 0;
}
